import plugin from '../../../lib/plugins/plugin.js'
import { Config } from '../components/index.js'
import gsCfg from '../../genshin/model/gsCfg.js'
import fs from 'fs'
import YAML from 'yaml'
import { Common } from '../components/index.js'
import uploadRecord from '../../huasheng/model/uploadRecord.js'

const _path = process.cwd();
let 语音=''
let 名字='...........'
let gameing=false
let 计时={}
let _img={}
export class hs_cyy extends plugin {
    constructor() {
        super({
            name: 'huasheng猜语音',
            dsc: '',
            event: 'message',
            priority: 120,
            rule: [
                {
                    reg: '^#*猜语音(中文|日语|日文|韩语|韩文|外语|英语)?$',
                    fnc: 'cyy',
                },
                {
                    reg: '',
                    fnc: 'huida',
                    log: false
                },
            ]
        })
   }
    async cyy(e) {
     if(!Config.getConfig('set','pz')['cyy']){ return false}
     if(gameing){return e.reply('猜语音游戏进行中(˃ ⌑ ˂ഃ )')}
      let 类型=e.msg.replace(/#|猜语音/g,'')
        if(!类型||类型=='中文'){
        语音='中文'
        }
        else if(类型=='日文'||类型=='日语'){
        语音='日文'
        }
        else if(类型=='韩文'||类型=='韩语'){
        语音='韩文'
        }
        else if(类型=='外语'||类型=='英语'){
        语音='英文'
        }
        let ml='./plugins/huasheng/resources/原神全语音/'
        gameing=true
        let name = fs.readdirSync(ml);
        //获取随机名字
        let i=Math.ceil(Math.random() * name.length)-1
        名字=name[i].replace(/.yaml/g,'')
        let 路径='./plugins/huasheng/resources/原神全语音/'+名字+'.yaml'
        let 读取 = fs.readFileSync(路径, 'utf8');
        let 数据= YAML.parse(读取);
        //获取随机语音
        let k=Math.ceil(Math.random() * 数据.简述.length)-1;
     let 简述=数据.简述[k]
     let 详细=数据.详细[k]
     if(语音=='中文'){
     语音=数据.中文[k]
     }
     if(语音=='韩文'){
     语音=数据.韩文[k]
     }
     if(语音=='日文'){
     语音=数据.日文[k]
     }
     if(语音=='英文'){
     语音=数据.英文[k]
     }
     let gq=await redis.get('hs_gqyy')
     if(gq){
      await e.reply(await uploadRecord(`${语音}.mp3`,0,false))
       }else{
      await  e.reply(await segment.record(`${语音}.mp3`))
       }
      await e.reply('请直接回答角色名，45秒后公布答案')
      let sf='很遗憾！45秒内没有人答对~'
      let sfl='有小伙伴，答对了呢~'
     //渲染数据
    let data = {
    tplFile: `./plugins/huasheng/resources/yy/cyy.html`,
    toux: _path+`/plugins/huasheng/resources/角色头像/${名字}.webp`,
    名字:名字,
    简述:简述,
    详细:详细,
    sf:sf,
    dz: process.cwd()
    }
    let data1 = {
    tplFile: `./plugins/huasheng/resources/yy/cyy.html`,
    toux: _path+`/plugins/huasheng/resources/角色头像/${名字}.webp`,
    名字:名字,
    简述:简述,
    详细:详细,
    sf:sfl,
    dz: process.cwd()
    }
   let img = await Common.render('yy/cyy', {
		...data
	}, { e, scale: 2 });
	
  计时 = setTimeout(() => {  if(gameing) {
      gameing = false;
      e.reply(img)
    }
  }, 44600)//毫秒
  _img=await Common.render('yy/cyy', {
		...data1
	}, { e, scale: 2 });
    return true;
    }
    
    
    
  async huida(e) {
  if(!Config.getConfig('set','pz')['cyy']){ return false}
     if(!e.msg){
     return false
  }
     if(!gameing){
     return false
     }
     let 别名 = {}
     let mz={}
        //调用云崽的别名配置
        别名 = gsCfg.getRole(e.msg)
     if(别名.name != undefined & 别名.name != "主角"){
       mz = 别名.name
     if(mz==名字){
      e.reply(['恭喜你，你答对了',_img],true)
      clearTimeout(计时)
      gameing=false
      }
      return true
     }else{
      return false
     }
  
    }
    
 }